<?php
namespace app\api\controller;
use app\model\MessageModel;
use app\model\UserDouModel;
use app\model\UserModel;
use support\Request;
use think\facade\Db;
use tool\User;

class MyController extends Base
{
    // 反馈提交
    public function feedback(Request $request){
        $wenti=$request->post('wenti','');
        $desc=$request->post('desc','');
        $image=$request->post('image',[]);
        $phone=$request->post('phone','');
        $user=User::findMe();
        MessageModel::create( [
            'uid'=>$user['id'],
            'title'=>$wenti,
            'neirong'=>$desc,
            'image'=>implode(',',$image),
            'phone'=>$phone,
        ]);
        s();
    }
    //更新积分
    public function updateDou(Request $request){
        $user=User::findMe();
        //判断今天签到了吗
        $isQ=UserDouModel::where('uid',$user['id'])->whereBetweenTime('create_time', strtotime('today'),time())->find();
        if($isQ){
            e('今天已签到！');
        }

        $dou=10;
        //判断本周签到了几次
        $count=UserDouModel::where('uid',$user['id'])->whereBetweenTime('create_time', strtotime('Monday this week'),strtotime('Sunday this week'))->count();
        if($count>=6){
            $dou=60;
        }
        UserModel::where('id',$user['id'])->inc('dou',$dou)->update();
        UserDouModel::create([
            'uid'=>$user['id'],
            'dou'=>$dou,
            'type'=>1
        ]);
        s();
    }

    //获取积分信息
    public function getDou(Request $request){
        $user=User::findMe();
        $userInfo=UserModel::where('id',$user['id'])->find();
        if(!$userInfo){
            e('用户不存在！','',505);
        }
        // 获取当前日期和时间
        $currentDate = new \DateTime();
        // 计算本周一的日期
        $monday = clone $currentDate;
        $monday->modify('monday this week'); // 获取本周一
        // 计算本周日的日期
        $sunday = clone $monday;
        $sunday->modify('sunday this week'); // 获取本周日
        // 生成一周的日期范围
        $period = new \DatePeriod(
            $monday, // 开始日期
            new \DateInterval('P1D'), // 间隔为1天
            $sunday->modify('+1 day') // 结束日期（包含周日）
        );

        $arr=[];
        foreach ($period as $date) {
            $arr[] = [
              'date'=>  $date->format('Y-m-d'),
                's'=>$date->getTimestamp(),
                'e'=>$date->getTimestamp() + 86399,
                'isT'=>$date->format('Y-m-d') === $currentDate->format('Y-m-d')

            ];
        }
        $isSign=false;
        $n=date('N');
        $days=[];
        foreach ($arr as $k=>$v){
            $isQ=UserDouModel::where('uid',$user['id'])->whereBetweenTime('create_time', $v['s'],$v['e'])->find();
            if($isQ){
                $days[]=$k;
                if($v['isT']){
                    $isSign=true;
                }
            }
        }
        s([
            'days'=>$days,
            'n'=>$n,
            'score'=>$userInfo['dou'],
            'isSign'=>$isSign
        ]);


    }

    public function updateUserInfo(Request $request){
        $info=$request->post('user',[]);
        if(!$info or !is_array($info) or !$info['id']){
            e('数据错误！');
        }
		$info['uid']=$info['id'];
		$info['create_time']=time();
		$info['update_time']=time();
		 unset($info['id']);
        unset($info['sheng_html_arr']);
		Db::name('user_xiu')->insert($info);
        $user=UserModel::where('id',$info['uid'])->field('id,name,user_no,img,sex,sheng_html')->find();
        s([
            'userInfo'=>$user
        ]);
    }
    public function getUserInfo(Request $request){
        $user=User::findMe();
        if(!$user){
            e('登录后才能查看','','505');
        }
        $userModel=UserModel::field('id,name,img,user_no,phone,sex,address,sheng,shi,xian,sheng_html,desc')->find($user['id']);
        s($userModel);
    }
    public function update(Request $request){
        $token1=$request->post('token1','');
        if($token1!='jkklx.20x5'){
           e('最新版本无需升级');
        }
        $path = public_path().'/update';
        $url='http://'.$request->host();
        $arr=[];
        $files = scandir($path);
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                $hou=pathinfo($file);
                if(isset($hou['extension']) and $hou['extension']=='apk'){
                    $arr[]= $hou['filename'];
                }
            }
        }
        rsort($arr);
        if(empty($arr)){
           e('最新版本无需升级');
        }else{
            s([
                'desc'=>'修改bug。如果升级缓慢，请错峰升级！',
                'url'=>$url.'/update/'.$arr[0].'.apk',
                'app_v'=>(int)$arr[0],
            ]);
        }
    }

}
